package com.freecloud.whispered.auth.service.impl;

import com.freecloud.framework.service.AbstractBaseService;
import com.freecloud.whispered.auth.entity.SysRoleMenuEntity;
import com.freecloud.whispered.auth.repository.SysRoleMenuDao;
import com.freecloud.whispered.auth.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 角色与菜单对应关系
 * 
 * @author maomao
 * @email 138383877@qq.com
 */
@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends AbstractBaseService<SysRoleMenuEntity,Long,SysRoleMenuDao> implements SysRoleMenuService {
	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		if(menuIdList.size() == 0){
			return ;
		}
		//先删除角色与菜单关系
		sysRoleMenuDao.deleteByRoleId(roleId);

		List<SysRoleMenuEntity> list = new ArrayList<>(menuIdList.size());

		menuIdList.stream().forEach(e ->{
			SysRoleMenuEntity entity = new SysRoleMenuEntity();
			entity.setMenuId(e);
			entity.setRoleId(roleId);
		});

		sysRoleMenuDao.saveAll(list);
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return sysRoleMenuDao.queryMenuIdList(roleId);
	}

}
